
package com.struts2.action;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;


public class UploadPhotoAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	private String pagePath; 
	private String imageChar;
	
	private HttpServletRequest servletRequest;

	@SuppressWarnings("deprecation")
	public String saveSrcPhoto() throws Exception {
		
		String filePath = servletRequest.getSession().getServletContext().getRealPath(getSavePath());
	    System.out.println("Server path:" + filePath);
		//set file name
		/*String imageFileName = new Date().getTime()
				+ getExtention(uploadFileName);*/
		setPagePath(getSavePath() + "/" + uploadFileName);
		File fileToCreate = new File(filePath, this.uploadFileName);
	    System.out.println("uploadFileName" + uploadFileName);
	    FileUtils.copyFile(this.upload, fileToCreate);
		//copy(upload, new File(path));
		System.out.println("upload           " + upload);
		
		return "saveSrcPhotoSuccess";
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getPagePath() {
		return pagePath;
	}

	public void setPagePath(String pagePath) {
		this.pagePath = pagePath;
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String getImageChar() {
		return imageChar;
	}

	public void setImageChar(String imageChar) {
		this.imageChar = imageChar;
	}
	
}
